﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GameLogic.Objects
{
    class Bullet : MovingObject
    {
        public Bullet(Tank creator)
        {
            HitPoints = Constants.HITS_FOR_BULLET;//на случай бронебойных пуль, летящих через всю карту
            Creator = creator;
        }

        private int hitPoints;

        public event EventHandler<BlowEventArgs> Blow;

        public override int HitPoints
        {
            get
            {
                return hitPoints;
            }
            set
            {
                hitPoints = value;
            }
        }

        public bool IsPlayerBullet { get; set; }
        public Tank Creator { get; set; }
        public override void Move(Map map)
        {
            Point point = new Point(-100, -100);
            Size size = new Size(Constants.BULLET_SIZE, Constants.BULLET_SIZE);
            var body = GetPhysRectangle();
            Rectangle newPosition = Position;
            switch (CurrentDirection)
            {
                case Direction.NORTH:
                    point = new Point(body.X, body.Y - Constants.BULLET_SPEED);
                    newPosition = new Rectangle(Position.X, Position.Y - Constants.BULLET_SPEED, Constants.BULLET_SIZE, Constants.BULLET_SIZE);
                    break;
                case Direction.EAST:
                    point = new Point(body.X + Constants.BULLET_SPEED, body.Y);
                    newPosition = new Rectangle(Position.X + Constants.BULLET_SPEED, Position.Y, Constants.BULLET_SIZE, Constants.BULLET_SIZE);
                    break;
                case Direction.SOUTH:
                    point = new Point(body.X, body.Y + Constants.BULLET_SPEED);
                    newPosition = new Rectangle(Position.X, Position.Y + Constants.BULLET_SPEED, Constants.BULLET_SIZE, Constants.BULLET_SIZE);
                    break;
                case Direction.WEST:
                    point = new Point(body.X - Constants.BULLET_SPEED, body.Y);
                    newPosition = new Rectangle(Position.X - Constants.BULLET_SPEED, Position.Y, Constants.BULLET_SIZE, Constants.BULLET_SIZE);
                    break;
            }
            Rectangle newPhysicalPos = new Rectangle(point, size);
            var flewTo = GetCollisedObject(newPhysicalPos, map);
            if (flewTo != null)
            {
                OnHit(flewTo);
            }
            else
            {
                Position = newPosition;
            }
        }

        public override Rectangle GetPhysRectangle()
        {
            return new Rectangle(Position.X + 25, Position.Y + 25,
                Constants.BULLET_SIZE, Constants.BULLET_SIZE);
        }

        public override void Draw(Graphics graphics)
        {
            Image image = GetCurrentImage();
            var place = GetPhysRectangle();
            DrawByPicture(graphics, image, place);
        }

        public override Image GetCurrentImage()
        {
            switch (CurrentDirection)
            {
                case Direction.NORTH:
                    return PictureManager.GetImage(ImageKind.BULLET_NORTH);
                case Direction.EAST:
                    return PictureManager.GetImage(ImageKind.BULLET_EAST);
                case Direction.SOUTH:
                    return PictureManager.GetImage(ImageKind.BULLET_SOUTH);
                case Direction.WEST:
                default:
                    return PictureManager.GetImage(ImageKind.BULLET_WEST);
            }
        }

        public void OnHit(GameObject obj)
        {
            HitPoints--;
            if (HitPoints == 0)
            {
                BlowEventArgs args = new BlowEventArgs()
                {
                    Target = obj
                };
                if (Blow != null)
                    Blow(this, args);
            }
        }

        public GameObject GetCollisedObject(Rectangle newPos, Map map)
        {
            var objects = map.Objects;
            foreach (var gameObject in objects)
            {
                if (gameObject != this && newPos.IntersectsWith(gameObject.GetPhysRectangle()))
                {
                    return gameObject;
                }
            }
            return null;
        }
    }
}
